package com.xuecheng.base.model;

import lombok.Data;
import lombok.ToString;

/**
 * @Author:王亚栋
 * @Date: 2023/7/21 下午9:21
 * @Version:1.0
 */
@Data
@ToString
public class RestResponse<T> {

    /**
     * 响应码 0正常 -1错误
     */
    private int code;

    /**
     * 响应提示信息
     */
    private String msg;

    private T result;

    public RestResponse(){

        this(0,"success");
    }

    public RestResponse(int code,String msg){

        this.code = code;
        this.msg = msg;
    }

    /**
     * 添加正常响应数据
     * @param result
     * @param <T>
     * @return
     */
    public static <T> RestResponse<T> success(T result){

        RestResponse<T> response = new RestResponse<>();

        response.setResult(result);

        return response;

    }

    public static <T> RestResponse<T> success(T result,String msg){

        RestResponse<T> response = new RestResponse<>();

        response.setResult(result);
        response.setMsg(msg);

        return response;

    }

    public static <T> RestResponse<T> validfail(String msg){

        RestResponse<T> response = new RestResponse<>();

        response.setCode(-1);

        response.setMsg(msg);

        return response;

    }

    public static <T> RestResponse<T> validfail(T result,String msg){

        RestResponse<T> response = new RestResponse<>();

        response.setCode(-1);

        response.setResult(result);

        response.setMsg(msg);

        return response;

    }
}
